import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Post,
  Request,
} from "@nestjs/common";
import { CreateQualityDto } from "src/modules/quality/dto/create-quality.dto";
import { QualityService } from "src/modules/quality/quality.service";
import { Roles } from "src/utils/decorators/roles.guard";
import { ApiBearerAuth, ApiTags } from "@nestjs/swagger";

@Controller("quality")
@ApiTags("商品质量")
@ApiBearerAuth()
export class QualityController {
  constructor(private readonly qualityService: QualityService) {}

  // 列出所有质量列表，如果是管理员，列出所有，如果不是，列出当前用户所在公司的所有
  @Get("list")
  list(@Request() req) {
    if (req.user.aud === "admin") {
      return this.qualityService.findAll();
    } else {
      const companyId = req.user.company;
      return this.qualityService.findByCompany(companyId);
    }
  }

  // 为当前公司创建质量，只有经理拥有该权限
  @Post()
  create(@Request() req, @Body() createQualityDto: CreateQualityDto) {
    const companyId = req.user.company;
    return this.qualityService.create(createQualityDto, companyId);
  }

  // 列出公司id对应的所有质量列表
  @Get("list/:companyId")
  @Roles("admin")
  listByCompany(@Param("companyId") companyId: string) {
    return this.qualityService.findByCompany(companyId);
  }

  // 根据ID查找质量
  @Get(":id")
  find(@Param("id") id: string) {
    return this.qualityService.find(id);
  }

  // 删除质量，只有经理拥有该权限
  @Delete(":id")
  @Roles("manager", "admin")
  remove(@Param("id") id: string) {
    return this.qualityService.remove(id);
  }
}
